<template>
  <div>
    <!-- 权限点数据展示 -->
    <el-tree
      ref="refTree"
      :data="permissionData"
      :props="{ label: 'name' }"
      default-expand-all
      :show-checkbox="true"
      :check-strictly="true"
      node-key="id"
    />
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSave">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { arrayToTree } from '@/utils/index'
import { getRoleDetail, assignPerm } from '@/api/setting'
export default {
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      permissionData: [] // 存储权限数据
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {

    async loadPermissionList() {
      // 发送请求, 获取权限列表
      const { data } = await getPermissionList()
      console.log('权限列表的数据是', data)
      this.permissionData = arrayToTree(data)
    },
    async loadPermissionByRoleId() {
    // 根据roleId获取当前这个角色已经具备的权限
      const res = await getRoleDetail(this.roleId)
      console.log('获取当前角色的已有的权限点数据', res.data)
      this.$refs.refTree.setCheckedKeys(res.data.permIds)
    },
    // 点击确定
    async hSave() {
      const permIds = this.$refs.refTree.getCheckedKeys()
      await assignPerm({
        id: this.roleId,
        permIds
      })
      this.$message.success('保存角色的权限成功')
      this.$emit('close') // 关闭弹框
    },
    // 点击取消
    closeDialog() {
      this.$emit('close')
    }
  }
}
</script>

<style>

</style>
